# -*- coding: utf-8 -*-
import os
import scrapy
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.pipelines.images import ImagesPipeline


class BaiduimgPipeline(object):
    num = 0

    def process_item(self, item, spider):
        self.num += 1
        if not os.path.exists('scrapycat'):
            os.mkdir('scrapycat')
        with open('scrapycat' + '/' + str(self.num) + ".jpg", "wb") as f:
            f.write(item['content'])
        return item


# 定义媒体管道类
class ImgPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):

        return [scrapy.Request(x) for x in item.get(self.images_urls_field,[])]
    pass
